﻿using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.UI;

namespace Nirvana
{
    public class CircleImage : Image
    {
        [Range(4f, 360f)]
        [SerializeField]
        private int segmentCount = 36;
        [Range(-100f, 100f)]
        [SerializeField]
        private int fillPercent = 100;

        public int SegmentCount
        {
            get
            {
                return this.segmentCount;
            }
            set
            {
                if (this.segmentCount != value)
                {
                    this.segmentCount = value;
                    this.SetVerticesDirty();
#if UNITY_EDITOR
                    EditorUtility.SetDirty(base.transform);
#endif
                }
            }
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            vh.Clear();
            var pivot = this.rectTransform.pivot;
            var rect = this.rectTransform.rect;
            float tw = rect.width;
            float th = rect.height;
            float startX = -pivot.x * rect.width;

            Vector4 uv = overrideSprite != null ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;
            float uvCenterX = (uv.x + uv.z) * 0.5f;
            float uvCenterY = (uv.y + uv.w) * 0.5f;
            float uvScaleX = (uv.z - uv.x) / tw;
            float uvScaleY = (uv.w - uv.y) / th;

            float curSegements = this.fillPercent * 1f / 100 * 2 * Mathf.PI / (float)this.segmentCount;

            float angle = 0;
            Vector2 vect = Vector2.zero;
            Vector2 uv2 = new Vector2(uvCenterX, uvCenterY);

            for (int i = 0, imax = this.segmentCount + 1; i < imax; i++)
            {
                float cosA = Mathf.Cos(angle);
                float sinA = Mathf.Sin(angle);
                Vector2 oldpos = vect;
                Vector2 newpos = new Vector2(startX * cosA, startX * sinA);
                vect = newpos;

                Vector2 uv0 = new Vector2(oldpos.x * uvScaleX + uvCenterX, oldpos.y * uvScaleY + uvCenterY);
                Vector2 uv1 = new Vector2(newpos.x * uvScaleX + uvCenterX, newpos.y * uvScaleY + uvCenterY);

                UIVertex[] uivertexs = new UIVertex[4];
                UIVertex v1 = default(UIVertex);
                v1.color = this.color;
                v1.position = oldpos;
                v1.uv0 = uv0;
                uivertexs[0] = v1;

                UIVertex v2 = default(UIVertex);
                v2.color = this.color;
                v2.position = newpos;
                v2.uv0 = uv1;
                uivertexs[1] = v2;

                UIVertex v3 = default(UIVertex);
                v3.color = this.color;
                v3.position = Vector2.zero;
                v3.uv0 = uv2;
                uivertexs[2] = v3;

                UIVertex v4 = default(UIVertex);
                v4.color = this.color;
                v4.position = Vector2.zero;
                v4.uv0 = uv2;
                uivertexs[3] = v4;

                vh.AddUIVertexQuad(uivertexs);
                angle += curSegements;

            }

        }

    }
}

